<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\api\controller;

use Mscore\Core\Controller;
use Mscore\Core\Lib\Captcha;
use Mscore\Core\Lang;
use app\api\model\Contract as ContractModel;
use app\api\model\Common as CommonModel;
use app\api\model\Login;

class LoginController extends Controller
{
    private $header;

    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new Login;
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])&&!empty($this->header['language'])?$this->header['language']:config('langs.default');
        Lang::init($this->header['language']);
    }

    /**
     * 登录
     * @param array $login 
     * @param array $where 
    */
    public function login(){
        $info = $this->model->userAuth(post('username',false),post('passwd',false),post('ip',false));
        $this->echoJson($info);
    }

    /**
     * 注册
     * @param array $register 
     * @param array $where 
    */
    public function register(){
        $regset=(new CommonModel())->getBaseinfo('keyname="edituser"','content');
        $regset=isset($regset['content'])&&!empty($regset['content'])?$regset['content']:[];
        $params = post();
        $usercode = post('usercode');
        if(empty($usercode)&&$regset['usercode']==1) $this->echoJson(Lang::get('Usercodeempty'));
        if(empty($params['username'])) $this->echoJson(Lang::get('Accountempty'));
        if(empty($params['password'])) $this->echoJson(Lang::get('Passwordempty'));
        if(empty($params['repassword'])) $this->echoJson(Lang::get('Confirmempty'));
        if(empty($params['mobile'])&&$regset['mobile']==1) $this->echoJson(Lang::get('Mobileempty'));
        if(empty($params['email'])&&$regset['email']==1) $this->echoJson(Lang::get('Emailempty'));
        if($params['password'] != $params['repassword']) $this->echoJson(Lang::get('Duplicate'));
        if(!empty($params['code'])){
            //验证驗證码
            $st = (new CommonModel)->getcode($params['code']);
            if (!isset($st) || $st<1) {
                $this->error = Lang::get('Codeerror');
                return $this->echojson($this->error);
            }
        }
        if(!empty($usercode)){
            $params['pid']=$this->model->getAccID('usercode="'.$usercode.'"');
            if($regset['usercode']==1&&empty($params['pid'])){
                $this->echoJson(Lang::get('Usercodeerror'));
            }
        }
        $info = $this->model->register($params);
        $this->echoJson($info);
    }
    
     /**
     * 找回密码
     * @param array $findpwd 
     * @param array $where 
    */
    public function findpwd(){
        $params = post();
        if(empty($params['code'])) $this->echoJson(Lang::get('Codeempty'));
        if(empty($params['email'])|| empty($params['mobile'])) $this->echoJson(Lang::get('Parerror'));
        if(empty($params['newpassword'])) $this->echoJson(Lang::get('Passwordempty'));
        if(empty($params['repassword'])) $this->echoJson(Lang::get('Confirmempty'));
        if($params['newpassword'] != $params['repassword']) $this->echoJson(Lang::get('Duplicate'));
        //验证驗證码
        $st = (new CommonModel)->getcode($params['code']);
        if (!isset($st) || $st<1) {
            $this->error = Lang::get('Codeerror');
            return $this->echojson($this->error);
        }
        unset($params['oldpassword'],$params['repassword'],$password);
        $info = $this->model->updatepwd($params);
        return $this->echojson($info);
    }
    
    /**
     * 获取token
     * @param array $gettoken 
     * @param array $where 
    */
    public function gettoken(){
        $info = (new Login)->getToken();
        $this->echoJson($info);
    }
    
    /**
     * 登出
     * @param array $logout 
     * @param array $where 
    */
    public function logout(){
        $login = (new Login)->logout(post('ip',false));
        $this->echoJson($login);
    }
    
    /**
     * 获取協議
     * @param array $getreg 
     * @param array $where 
    */
    public function getreg(){
        $info = (new ContractModel)->cate(' a.acode="reg"');
        $info =isset($info['data'][0])?$info['data'][0]:[];
        if(!empty($info)){
            if(!empty($info['titles'])){
                $info['title']=isset(json_decode($info['titles'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($info['titles'],true)[$this->header['language']]):$info['title'];
            }
            if(!empty($info['content'])){
                $info['content']=isset(json_decode($info['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($info['content'],true)[$this->header['language']]):'';
            }
            unset($info['titles'],$info['introduction']);
        }
        $success=Lang::get('Success');
        $success['data']=$info;
        $this->echoJson($success);
    }

    /**
     * 获取注册方式
     * @param array $getregset 
     * @param array $where 
    */
    public function getregset(){
        $regset=(new CommonModel())->getBaseinfo('keyname="edituser"','content');
        $regset=isset($regset['content'])&&!empty($regset['content'])?$regset['content']:[];
        $success=Lang::get('Success');
        $success['data']=$regset;
        return $this->echojson($success); 
    }
    
}